package com.yyfax.ph.common.imageselector.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.yyfax.ph.R;
import com.yyfax.ph.common.imageselector.bean.Image;

import java.util.ArrayList;
import java.util.List;

public class ImageGridAdapter extends BaseAdapter {
	private static final int TYPE_CAMERA = 0;
	private static final int TYPE_NORMAL = 1;

	private boolean mShowCamera = true;
	private boolean mShowSelectIndicator = true;
	private boolean mIsSingleMode = false;

	private List<Image> mImages = new ArrayList<>();
	private List<Image> mSelectedImages = new ArrayList<>();
	private RequestManager mRequestManager;

	public ImageGridAdapter(Context context, boolean showCamera) {
		mShowCamera = showCamera;
		mRequestManager = Glide.with(context);
	}

	public void clearSelected() {
		mSelectedImages.clear();
		notifyDataSetChanged();
	}

	public void setSingleSelect(boolean singleFalg) {
		mIsSingleMode = singleFalg;
	}

	/**
	 * 显示选择指示器
	 */
	public void showSelectIndicator(boolean b) {
		mShowSelectIndicator = b;
	}

	public void setShowCamera(boolean b) {
		if (mShowCamera == b) return;
		mShowCamera = b;
		notifyDataSetChanged();
	}

	public boolean isShowCamera() {
		return mShowCamera;
	}

	/**
	 * 选择某个图片，改变选择状态
	 *
	 * @param image 选中的图片
	 */
	public void select(Image image) {
		if (mIsSingleMode) {
			if (null != mSelectedImages) {
				if (mSelectedImages.size() > 0)
					mSelectedImages.clear();
				mSelectedImages.add(image);
			}
		} else {
			if (mSelectedImages.contains(image)) {
				mSelectedImages.remove(image);
			} else {
				mSelectedImages.add(image);
			}
		}
		notifyDataSetChanged();
	}

	/**
	 * 通过图片路径设置默认选择
	 *
	 * @param resultList 结果集
	 */
	public void setDefaultSelected(ArrayList<String> resultList) {
		if (mSelectedImages != null && mSelectedImages.size() > 0) {
			mSelectedImages.clear();
		}
		if (resultList != null && resultList.size() > 0) {
			for (String path : resultList) {
				Image image = getImageByPath(path);
				if (image != null) {
					mSelectedImages.add(image);
				}
			}
		}
		notifyDataSetChanged();
	}

	private Image getImageByPath(String path) {
		if (mImages != null && mImages.size() > 0) {
			for (Image image : mImages) {
				if (image.path.equalsIgnoreCase(path)) {
					return image;
				}
			}
		}
		return null;
	}

	/**
	 * 设置数据集
	 *
	 * @param images 图片集合
	 */
	public void setData(List<Image> images) {
		mSelectedImages.clear();

		if (images != null && images.size() > 0) {
			mImages = images;
		} else {
			mImages.clear();
		}
		notifyDataSetChanged();
	}

	@Override
	public int getViewTypeCount() {
		return 2;
	}

	@Override
	public int getItemViewType(int position) {
		if (mShowCamera) {
			return position == 0 ? TYPE_CAMERA : TYPE_NORMAL;
		}
		return TYPE_NORMAL;
	}

	@Override
	public int getCount() {
		return mShowCamera ? mImages.size() + 1 : mImages.size();
	}

	@Override
	public Image getItem(int i) {
		if (mShowCamera) {
			if (i == 0) {
				return null;
			}
			return mImages.get(i - 1);
		} else {
			return mImages.get(i);
		}
	}

	@Override
	public long getItemId(int i) {
		return i;
	}

	@Override
	public View getView(int i, View view, ViewGroup viewGroup) {
		LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());

		int type = getItemViewType(i);
		if (type == TYPE_CAMERA) {
			view = inflater.inflate(R.layout.mis_view_list_item_camera, viewGroup, false);
			view.setTag(null);
		} else if (type == TYPE_NORMAL) {
			ViewHolder holder;
			if (view == null) {
				view = inflater.inflate(R.layout.mis_view_list_item_image, viewGroup, false);
				holder = new ViewHolder(view);
			} else {
				holder = (ViewHolder) view.getTag();
				if (holder == null) {
					view = inflater.inflate(R.layout.mis_view_list_item_image, viewGroup, false);
					holder = new ViewHolder(view);
				}
			}
			holder.bindData(getItem(i));
		}
		return view;
	}

	class ViewHolder {
		ImageView image;
		ImageView indicator;

		ViewHolder(View view) {
			image = (ImageView) view.findViewById(R.id.image);
			indicator = (ImageView) view.findViewById(R.id.checkmark);
			view.setTag(this);
		}

		void bindData(final Image data) {
			if (data == null) return;
			// 处理单选和多选状态
			if (mShowSelectIndicator && data.length > 0) {
				indicator.setVisibility(View.VISIBLE);
				if (mSelectedImages.contains(data)) {
					// 设置选中状态
					indicator.setImageResource(R.drawable.ic_radio_selected);
				} else {
					// 未选择
					indicator.setImageResource(R.drawable.mis_skin_icon_btn_unselected);
				}
			} else {
				indicator.setVisibility(View.GONE);
			}
			mRequestManager.load(data.path).placeholder(R.drawable.mis_skin_icon_default_error).error(R.drawable.mis_skin_icon_default_error).centerCrop().into(image);
		}
	}
}
